package com.learn.algorithm_771;

import java.util.HashSet;
import java.util.Set;

/**
 * 771. 宝石与石头
 * https://leetcode-cn.com/problems/jewels-and-stones/
 * @author gatts, 2020/8/23 0:16
 */
public class Solution {

    //- 方法一，暴力遍历
    public int numJewelsInStones(String J, String S) {
        char[] jewels = J.toCharArray();
        char[] stones = S.toCharArray();
        int count = 0;
        //- 时间复杂度 jewels.length * stones.length
        for (char stone : stones) {
            for (char jewel : jewels) {
                if (stone == jewel){
                    count++;
                }
            }
        }
        return count;
    }

    //- 方法二
    public int numJewelsInStones2(String J, String S) {
        int count = 0;
        //- J去重
        Set<Character> jewelSet = new HashSet<>();
        for (char c : J.toCharArray()) {
            jewelSet.add(c);
        }
        for (Character s : S.toCharArray()) {
            if (jewelSet.contains(s)){
                count ++;
            }
        }
        return count;
    }


}
